<?php
namespace  App\Models;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Utility\Schema\Table;

/**
 * Created by PhpStorm.
 * User: zq2020
 * Date: 20-2-26
 * Time: 下午11:45
 */
class User extends AbstractModel
{
    /**
     * 表的获取
     * 此处需要返回一个 EasySwoole\ORM\Utility\Schema\Table
     * @return Table
     */
    protected $tableName="easy_user";
    protected $connectionName="default";
    protected $autoTimeStamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    public function schemaInfo(bool $isCache=true):Table{
        $table = new Table($this->tableName);
        $table->colInt('id')->setIsPrimaryKey(true)->setIsAutoIncrement();
        $table->colVarChar('name', 255);
        $table->colVarChar('phone',255);
        $table->colDate("create_time");
        $table->colDate("update_time");
        return $table;
    }
    public function subUser()
    {
        return $this->hasOne(SubUser::class,function (QueryBuilder $query){
            $query->fields(['money','user_id']);
        },'id','user_id');
    }

    protected static function onBeforeInsert($model)
    {
        return $model->name ='啊';
        //return self::$insert;
    }

    protected static function onAfterInsert($model, $res)
    {
        //var_dump("1");

    }

    protected static function onBeforeUpdate($model)
    {
      //  return self::$update;
    }

    protected static function onAfterUpdate($model, $res)
    {

    }

    protected static function onBeforeDelete($model)
    {
        return self::$delete;
    }

    public static function onAfterDelete($model, $res)
    {

    }
}
